package others.swing.p012;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

class Change extends Frame implements ActionListener {
    Label lbl1, lbl2, lbl3;//定义标签
    Box baseBox, box1, box2;//定义Box
    TextField tf1, tf2;//定义文本框
    Button btn;//定义按钮

    Change() {
        super("修改密码界面！");//设置窗体标题
        this.setFont(new Font("隶书", Font.BOLD, 18));//设置界面的字体
        this.setBackground(Color.pink);//设置界面背景颜色
        //初始化标签
        lbl1 = new Label("修改密码界面");
        lbl2 = new Label("输入新密码：");
        lbl3 = new Label(" 再次输入:");
        //初始化文本框
        tf1 = new TextField(8);
        tf1.setEchoChar('*');
        tf2 = new TextField(8);
        tf2.setEchoChar('*');
        //初始化按钮
        btn = new Button("确定");
        //将各个控件加入box1
        box1 = Box.createVerticalBox();
        box1.add(lbl2);
        box1.add(Box.createVerticalStrut(8));
        box1.add(lbl3);
        box1.add(Box.createVerticalStrut(8));
        //将各个控件加入box2
        box2 = Box.createVerticalBox();
        box2.add(tf1);
        box2.add(Box.createVerticalStrut(8));
        box2.add(tf2);
        box2.add(Box.createVerticalStrut(8));
        //将box1,box2加入basebox
        baseBox = Box.createHorizontalBox();
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(5));
        baseBox.add(box2);

        setLayout(new FlowLayout());//设置窗体上的各控件的布局为流式布局
        //将控件加入界面
        add(lbl1);
        add(baseBox);
        add(btn);
        //为个控件注册监听对象
        tf1.addActionListener(this);
        tf2.addActionListener(this);
        btn.addActionListener(this);
        //采用匿名类实现窗体标题栏上“×”按钮的关闭功能
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent ew) {
                dispose();   //关闭窗口并释放所占屏幕资源
            }
        });
        setBounds(200, 200, 295, 215);//设置窗体的起始坐标，以及宽度和高度
        setVisible(true);//让窗体可见
        setResizable(false);//让窗体不可修改
    }

    @Override
    public void actionPerformed(ActionEvent ea) {
        //连接数据库
        Connection con;
        Statement sql;
        ResultSet rs;
        String user = Student.name;
        String pwd, s1, updateStr;
        pwd = tf1.getText();
        s1 = tf2.getText();

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:odbc:dormitory2", "", "");
            sql = con.createStatement();
            //修改User中与该学生对应的密码
            if (ea.getSource() == btn) {
                if (pwd.equals(s1)) {
                    updateStr = "UPDATE Users SET pwd=" + "'" + pwd + "'" + " WHERE name=" + "'" + user + "'";
                    sql.executeUpdate(updateStr);
                    dispose();   //关闭窗口并释放所占屏幕资源
                }
            }
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}